#!/bin/bash
set -e

# ABF token
TOKEN="${TOKEN:-}"
mkdir -p gits
mkdir -p results
dir0="$PWD"
cd gits

while read -r line
do
	rm -fr "$line"
	if ! git clone git@abf.io:import/"$line".git -b rosa2021.15; then
		echo "$line" >> "$dir0"/results/clone-errors.list
		continue
	fi
	test -d "$line"
	pushd "$line"
		if [ "$(git diff origin/rosa2021.15..origin/rosa2021.1 | grep -c .)" = 0 ]; then
			echo "$line" >> "$dir0"/results/no-diff.list
			popd
			continue
		fi
		if git merge origin/rosa2021.1
		then
			echo "$line" >> "$dir0"/results/merged.list
		else
			echo "$line" >> "$dir0"/results/merge-errors.list
		fi
		git push || echo "$line" >> "$dir0"/results/push-errors.list
	popd
	echo "$line" >> "$dir0"/results/done.list
done < <(cat "$dir0"/srpms.list)
